package com.simplefan.system.common.Rest;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.simplefan.framework.common.rest.RestAnalyzerException;
import com.simplefan.framework.common.rest.RestListAnalyzer;
import com.simplefan.framework.common.rest.RestObjectAnalyzer;
import com.simplefan.framework.common.rest.RestObjectAnalyzerFactory;


public class ListRestObjectAnalyzer implements RestListAnalyzer{
	

	  private static final String XML_DQM = "\"";
	  private static final String XML_NL = "\n";
	  private static final String XML_QM = "\'";
	  private static final String TXML_DQM = "###";
	  private static final String TXML_NL = "!!!";
	  private static final String TXML_QM = "&&&";
	  
	  public static String txmlToXml(String txml) {
	      String xml = txml;
	      if (txml != null) {
	          xml = xml.replaceAll(TXML_DQM, XML_DQM);
	          xml = xml.replaceAll(TXML_NL, XML_NL);
	          xml = xml.replaceAll(TXML_QM, XML_QM);
	      }
	      return xml;
	  }

  @SuppressWarnings("unchecked")
  public List toValue(String xmlFragment, String itemType) {
    if (xmlFragment != null && !xmlFragment.equals("null")) {
      if (itemType != null && itemType.equals("metaobject")) {
        xmlFragment = txmlToXml(xmlFragment);
      }
      JSONArray jsonArray = JSONArray.fromString(xmlFragment);

      List result = new ArrayList();
      
      for (int i = 0; i < jsonArray.length(); i++) {
        if (itemType != null && itemType.equals("metaobject")) {
          JSONObject jsonObject = jsonArray.getJSONObject(i);
          RestObjectAnalyzer analyzer = RestObjectAnalyzerFactory.getAnalyzer(itemType);
          Object v = analyzer.toValue(jsonObject.toString());
          result.add(v);
        }
        else {
          Object object = jsonArray.get(i);
          RestObjectAnalyzer analyzer = RestObjectAnalyzerFactory.getAnalyzer(itemType);
          if(analyzer == null) {
            throw new RestAnalyzerException("Can not find an analyzer for parameter type " + itemType);
          }
          Object v = analyzer.toValue(object.toString());
          result.add(v);
        }
      }
      
      return result;
    }
    else {
      return null;
    }
  }

  @SuppressWarnings("unchecked")
  public String toXmlFragment(List value, String itemType) {
    if (value != null) {
      if (itemType != null && itemType.equals("metaobject")) {
        RestObjectAnalyzer analyzer = RestObjectAnalyzerFactory.getAnalyzer(itemType);
        boolean isBegin = true;
        StringBuffer buffer = new StringBuffer();
        buffer.append("[");
        for (Object v : value) {
          if (!isBegin) {
            buffer.append(",");
          }
          String singleValue = analyzer.toXmlFragment(v);
          buffer.append(singleValue);
          isBegin = false;
        }
        buffer.append("]");
        return buffer.toString();
      }
      else {
        return JSONArray.fromCollection(value).toString();
      }
    }
    else {
      return "null";
    }
  }
}
